from fastapi import APIRouter
from typing import List
from model.response import ResponseModel
from .model.request import SearchRequest, NoteRequest, UserRequest
from .model.response import NotesResult, NoteDetail, CommentInfo, UserInfo, Hots
from .service import XHSApiCore
from model.request import BaseRequest
router = APIRouter()

@router.post("/search", response_model=ResponseModel[NotesResult],description="搜索笔记")
async def search_notes(request: SearchRequest):
    """搜索笔记"""
    api_core = XHSApiCore()
    try:
        result, last_error,total_pages = await api_core.search_notes(search_request=request)
        return ResponseModel(
            code=200 if last_error else 400,
            msg= last_error if last_error else "success",
            data=result,
            execute_success_num=total_pages
        )
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        )

@router.post("/note/detail", response_model=ResponseModel[List[NoteDetail]], description="批量获取笔记详情")
async def get_note_detail(request: List[NoteRequest]):
    """批量获取笔记详情"""
    api_core = XHSApiCore()
    try:
        results, success_count, failed_msg = await api_core.get_note_detail(note_requests=request)
        
        # 如果所有请求都失败
        if not results:
            return ResponseModel(
                code=500,
                msg=failed_msg,
                data=[],
                execute_success_num=success_count
            )
        
        # 如果部分成功部分失败
        if failed_msg:
            return ResponseModel(
                code=206,  # Partial Content
                msg=failed_msg,
                data=results,
                execute_success_num=success_count
            )
            
        # 全部成功
        return ResponseModel(
            code=200,
            msg="success",
            data=results,
            execute_success_num=success_count
        )
        
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        )

@router.post("/hots", response_model=ResponseModel[List[Hots]],description="获取热门榜单")
async def get_hot_notes(request: BaseRequest):
    """获取热门榜单"""
    api_core = XHSApiCore()
    try:
        result = await api_core.get_hots(user_request=request)
        return ResponseModel(
            code=200,
            msg="success",
            data=result,
            execute_success_num=1
        )
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        ) 




@router.post("/user/info", response_model=ResponseModel[UserInfo],description="获取用户信息")
async def get_user_info(request: UserRequest):
    """获取用户信息"""
    api_core = XHSApiCore()
    try:
        result = await api_core.get_user_info(user_request=request)
        return ResponseModel(
            code=200,
            msg="success",
            data=UserInfo(**result)
        )
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        ) 

@router.post("/user/notes", response_model=ResponseModel[List[NoteDetail]],description="获取用户笔记")
async def get_user_notes(request: UserRequest):
    """获取用户笔记"""
    api_core = XHSApiCore()
    try:
        result = await api_core.get_user_info(user_request=request)
        return ResponseModel(
            code=200,
            msg="success",
            data=UserInfo(**result),
            execute_success_num=1
        )
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        ) 




@router.post("/note/comments", response_model=ResponseModel[List[CommentInfo]],description="获取笔记评论")
async def get_note_comments(request: NoteRequest):
    """获取笔记评论"""
    api_core = XHSApiCore()
    try:
        result = await api_core.get_note_comments(note_request=request)
        return ResponseModel(
            code=200,
            msg="success",
            data=[CommentInfo(**comment) for comment in result],
            execute_success_num=1
        )
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        )
    

@router.post("/note/sub_comments", response_model=ResponseModel[List[CommentInfo]],description="获取笔记子评论")
async def get_note_sub_comments(request: NoteRequest):
    """获取笔记子评论"""
    api_core = XHSApiCore()
    try:
        result = await api_core.get_note_sub_comments(note_request=request)
        return ResponseModel(
            code=200,
            msg="success",
            data=[CommentInfo(**comment) for comment in result],
            execute_success_num=1
        )
    except Exception as e:
        return ResponseModel(
            code=500,
            msg=str(e),
            data=None,
            execute_success_num=0
        )
    
    